Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
V2
/
Services
/
Filename :
TUITService.php
back
Copy
<?php namespace App\V2\Services; use App\Http\Controllers\Api\TUITWalletController; use App\Models\TUIT\TuitAllocationConfig; use App\Models\User; use Carbon\Carbon; use Exception; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class TUITService extends BaseService{ private $tuitWalletController; public function __construct(){ $this->tuitWalletController = new TUITWalletController(); } public function triggerTuitCreditAction(User $user, $action, $description = null, $tuit_points = null){ if(empty($action)){ Log::debug("action is empty, not performing any tuit operation"); return null; } $tuitAllocationConfig = TuitAllocationConfig::where('action', $action)->first(); if($tuitAllocationConfig == null){ Log::debug("didn't find any tuit allocation config for $action"); return null; } if($tuitAllocationConfig->type != 'CREDIT'){ Log::debug("TUIT action is not type of credit"); return null; } if($tuitAllocationConfig->action == 'MANUAL_ALLOCATION_BY_ADMIN'){ $tuitAllocationConfig->tuit_points = $tuit_points; } $transactionDetails = array( 'transaction_title' => "Action performed: $action", 'ref_table' => null, 'ref_table_id' => null, 'description' => $description == null ? "allocating points for completing $action" : $description, ); $tuitWalletTransaction = $this->tuitWalletController->creditTUIT($user, $tuitAllocationConfig->tuit_points, $transactionDetails); if($tuitWalletTransaction != null && !empty($tuitWalletTransaction)){ Log::info("{$tuitAllocationConfig->tuit_points} points allocated for $action to {$user->user_id}"); return $tuitWalletTransaction; } else { Log::debug("could not allocate points for some reason"); return null; } } public function triggerTuitDebitAction(User $user, $action, $description = null, $tuit_points = null){ if(empty($action)){ Log::debug("action is empty, not performing any tuit operation"); return null; } $tuitAllocationConfig = TuitAllocationConfig::where('action', $action)->first(); if($tuitAllocationConfig == null){ Log::debug("didn't find any tuit debit config for $action"); return null; } if($tuitAllocationConfig->type != 'DEBIT'){ Log::debug("TUIT action is not type of debit"); return null; } if($tuitAllocationConfig->action == 'MANUAL_DEDUCTION_BY_ADMIN'){ $tuitAllocationConfig->tuit_points = $tuit_points; } if(!$this->tuitWalletController->canDebitTUIT($user, $tuitAllocationConfig->tuit_points)){ throw new Exception('do not have enough balance to deduct TUIT'); } $transactionDetails = array( 'transaction_title' => "Action performed: $action", 'ref_table' => null, 'ref_table_id' => null, 'description' => $description == null ? "debiting points for $action" : $description, ); $tuitWalletTransaction = $this->tuitWalletController->debitTUIT($user, $tuitAllocationConfig->tuit_points, $transactionDetails); if($tuitWalletTransaction != null && !empty($tuitWalletTransaction)){ Log::info("{$tuitAllocationConfig->tuit_points} points debited for $action from user id {$user->user_id}"); return $tuitWalletTransaction; } else { Log::debug("could not debit points for some reason"); return null; } } public function getTUITWallet(User $user){ return $this->tuitWalletController->getTUITWallet($user); } public function getTUITLatestTransactions(User $user, $num_limit){ return $this->tuitWalletController->getTUITLatestTransactions($user, $num_limit); } public function getTUIT_To_USDT_old($tuit_value){ $alchemyApiKey = Config::get('constants.app.alchemy-api-key'); $routerAddress = Config::get('constants.app.uniswap-router-address'); $usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7'; $tuitAddress = '0x963cd3e835d81ce8e4ae4836e654336dab4298e9'; // Ethereum Router contract address $url = "https://eth-mainnet.alchemyapi.io/v2/$alchemyApiKey"; $routerAddress = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'; // // Get price of 1 USDT in WETH // $usdtInWeth = $this->getTokenPrice($url, $routerAddress, $usdtAddress); // // Get price of 1 TUIT in WETH // $tuitInWeth = $this->getTokenPrice($url, $routerAddress, $tuitAddress); $amountIn = 1; $tuitToUsdtPrice = $this->getTokenPrice($url, $routerAddress, $tuitAddress, $usdtAddress, $amountIn); // $usdtToTuitPrice = $usdtInWeth / $tuitInWeth; // Price of 1 USDT in terms of TUIT // Calculate price of 1 TUIT in terms of USDT // $tuitToUsdtPrice = $tuit_value * (1 / $usdtToTuitPrice); $tuitToUsdtPrice = $tuit_value * $tuitToUsdtPrice; return $tuitToUsdtPrice; } private function getTokenPrice($alchemyUrl, $routerAddress, $tokenIn, $tokenOut, $amountIn) { // Function to get amount out for 1 token unit $functionSelector = '0x0624f7dc'; $tokenOutWithoutPrefix = substr($tokenOut, 2); $paddedTokenOut = str_pad($tokenOutWithoutPrefix, 64, '0', STR_PAD_LEFT); $data = $functionSelector . $paddedTokenOut; $request_data = [ 'jsonrpc' => '2.0', 'method' => 'eth_call', 'params' => [ [ 'to' => $routerAddress, 'data' => $data, ], 'latest', ], 'id' => 1, ]; echo 'Request: ' . json_encode($request_data); // Make request to Ethereum node $response = Http::post($alchemyUrl, $request_data); // Parse response and return price in wei $result = $response->json(); echo 'Response: ' . json_encode($result);die; $amountOut = hexdec($result['result']); // Convert from wei to actual token amount $price = $amountOut / pow(10, 18); // Assuming tokens have 18 decimal places return $price; } // private function getTokenPrice($url, $routerAddress, $tokenAddress) // { // // Function to get amount out for 1 token unit // $data = [ // 'to' => $routerAddress, // 'data' => '0x0624f7dc' . str_pad(substr($tokenAddress, 2), 64, '0', STR_PAD_LEFT), // Function selector (0x0624f7dc) + token address // ]; // $request_data = [ // 'jsonrpc' => '2.0', // 'method' => 'eth_call', // 'params' => [ // [ // 'to' => $routerAddress, // 'data' => $data, // ], // 'latest', // ], // 'id' => 1, // ]; // echo 'Request: ' . json_encode($request_data); // // Make request to Ethereum node // $response = Http::post($url, $request_data); // // Parse response and return price in wei // $result = $response->json(); // echo 'Response: ' . json_encode($result);die; // $amountOut = hexdec($result['result']); // return $amountOut; // } public function getTUIT_To_USDT($tuit_value){ try{ $ratio = $this->getPrice(); return $tuit_value * $ratio; }catch(Exception $e){ Log::error("error while retriving price from dextools"); Log::error($e->getMessage()); Log::error($e->getTraceAsString()); return 0; } } private function getPrice() { $apiKey = Config::get('constants.app.dextools-api-key'); $pool = Config::get('constants.app.dextools-chain-name');; $address = Config::get('constants.app.dextools-pool-address');; $response = Http::withHeaders([ 'accept' => 'application/json', 'X-API-KEY' => $apiKey, ])->get("https://public-api.dextools.io/trial/v2/pool/{$pool}/{$address}/price"); $price = null; // Check if the request was successful if ($response->successful()) { $price = $response->json()['data']['price']; } else { // Handle unsuccessful response throw new Exception('could not get price from dextools'); } return $price; } } ?>